package com.aijk.web.admin.controller;



import com.aijk.ehm.sys.api.SysAdminApi;
import com.aijk.ehm.sys.entity.enums.AdminType;
import com.aijk.ehm.sys.model.SysModule;
import com.aijk.ehm.sys.model.qo.ModuleQo;
import com.aijk.web.admin.entity.MenuVo;
import com.aijk.web.admin.service.ModuleService;
import com.aijk.web.admin.shiro.ShiroDbRealm;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by iscdw on 2016/4/18.
 */
@Controller
@RequestMapping("/")
public class MainController {
    
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private SysAdminApi adminApi;
    @RequestMapping("/main")
    public String index(Model model) {

        ShiroDbRealm.ShiroUser user = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        List<SysModule> list = moduleService.getUserModuleList(user, 1);
        List<Integer> ids=new ArrayList<>();
        for (SysModule module : list) {
            if( 0 != module.getId())
            ids.add(module.getId());
            if( 0 != module.getPid())
            ids.add(module.getPid());
        }
        ModuleQo qo = new ModuleQo();
        if(ids.size()>0){
            qo.setIds(ids);
        }else{
            qo.setId(-1);
        }
        qo.setSortway(" orderno asc ");
        List<SysModule> ownsModules = moduleService.findAll(qo);
        //根据一级菜单id查询所有的菜单  
        List<MenuVo> modules = new ArrayList<>();
        for (SysModule module : ownsModules) {
            //这里需要的是一级菜单的id，对于没有父菜单id的就是一级菜单  
            MenuVo menuVo = new MenuVo();
            menuVo.setModule(module);
            if (0 == module.getPid()) {
                List<MenuVo> iterateMenus = iterateMenus(list, module.getId());
                menuVo.setChilds(iterateMenus);
                modules.add(menuVo);
            }
        }

        model.addAttribute("position",adminApi.get(user.getUserId()).getPosition());
        model.addAttribute("adminType", user.getType() == AdminType.SUPER_ADMIN.getValue() ? "超级管理员" : "普通管理员");
        model.addAttribute("modules", modules);
        return "/main";
    }

    @RequestMapping("/unauthorized")
    public String unauthorized() {

        return "error/unauthorized";
    }

    @RequestMapping("/welcome")
    public String welcome() {

        return "/welcome";
    }

    @RequestMapping("/builder")
    public String builder() {

        return "/tools/form_builder";
    }

    //多级菜单查询方法  
    public List<MenuVo> iterateMenus(List<SysModule> moduleList, Integer pid) {
        List<MenuVo> result = new ArrayList<>();

        for (SysModule module : moduleList) {
            MenuVo menuVo = new MenuVo();
            menuVo.setModule(module);
            Integer menuid = module.getId();//获取菜单的id  
            Integer parentid = module.getPid();//获取菜单的父id  
            if (parentid != null) {
                if (parentid.equals(pid)) {
                    List<MenuVo> iterateMenu = iterateMenus(moduleList, menuid);
                    menuVo.setChilds(iterateMenu);
                    result.add(menuVo);
                }
            }
        }
        return result;
    }
}
